package com.kaiyidiannao.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.Auction;
import com.kaiyidiannao.model.req.model.GetCloseOpen;
import com.kaiyidiannao.view.ScaleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mark on 2014/4/16.
 */
public class MyAuctionAdapter extends ImageLoaderBaseAdapter {

    private ImageLoader imageLoader;
    private LayoutInflater layoutInflater;
    private List<Auction> list = new ArrayList<Auction>();
    long seconds;
    private String[] imgs;
    private Context context;
    private int tip = 0;

    public MyAuctionAdapter(Context context, ArrayList<Auction> list,int tip) {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.tip = tip;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        TextView myAuctionItemNameText = null;
        TextView myAuctionItemPriceText = null;
        TextView increaseNumber = null;
        TextView increasePeopleNum = null;
        TextView pepoleNum;
        TextView pepoleName;
        ImageButton myAuctionItemButton = null;
        ScaleImageView auction_item_ImageView;
        LinearLayout textViewLinear,viewPepoleNum;
        Auction auction = list.get(position);
        if (convertView==null)
        {
            convertView=layoutInflater.inflate(R.layout.myauction_item,null);
        }
        myAuctionItemNameText = (TextView)convertView.findViewById(R.id.myAuctionItemNameText);
        myAuctionItemPriceText = (TextView)convertView.findViewById(R.id.myAuctionItemPriceText);
        increaseNumber = (TextView)convertView.findViewById(R.id.increaseNumber);
        increasePeopleNum = (TextView)convertView.findViewById(R.id.increasePeopleNum);
        myAuctionItemButton = (ImageButton)convertView.findViewById(R.id.myAuctionItemButton);
        auction_item_ImageView = (ScaleImageView)convertView.findViewById(R.id.my_auction_item_ImageView);
        textViewLinear = (LinearLayout)convertView.findViewById(R.id.textViewLinear);
        viewPepoleNum = (LinearLayout)convertView.findViewById(R.id.viewPepoleNum);
        pepoleNum = (TextView)convertView.findViewById(R.id.pepoleNum);
        pepoleName = (TextView)convertView.findViewById(R.id.pepoleName);
//        imgs = (auction.getThumb()+",").split(",");
//        imageLoader.displayImage(imgs[0], auction_item_ImageView);
        imageLoader.displayImage(auction.getThumb(),auction_item_ImageView, options, null);
        myAuctionItemNameText.setText(auction.getTitle());
        if (auction.getCurrprice() == null){
            myAuctionItemPriceText.setText("无");
        }else{
            myAuctionItemPriceText.setText(auction.getCurrprice()+"元");
        }

        if(auction.getCount() == null){
            increaseNumber.setText("无");
        }else{
            increaseNumber.setText(auction.getCount());
        }

        if(auction.getPeople() == null){
            increasePeopleNum.setText("无");
        }else{
            increasePeopleNum.setText(auction.getPeople());
        }


        if(list.get(position).getStatus()!=null){
            if (list.get(position).getStatus().equals("2")) {
                myAuctionItemButton.setImageBitmap(BitmapFactory.decodeResource(convertView.getResources(), R.drawable.mypublish_dark_button));
                textViewLinear.setVisibility(View.GONE);
                viewPepoleNum.setVisibility(View.VISIBLE);
                if(TextUtils.isEmpty(auction.getUsername()) || TextUtils.isEmpty(auction.getMobile())){
                    //pepoleNum.setText("暂无");
                    pepoleName.setText("暂无竞拍人");
                    pepoleNum.setVisibility(View.GONE);
                }else {
                    pepoleNum.setText(auction.getMobile());
                    pepoleName.setText(auction.getUsername());
                }
            }else {
                myAuctionItemButton.setImageBitmap(BitmapFactory.decodeResource(convertView.getResources(), R.drawable.mypublish_light_button));
                textViewLinear.setVisibility(View.VISIBLE);
                viewPepoleNum.setVisibility(View.GONE);
            }

        }

        if (null != convertView) {
            if (null != myAuctionItemButton) {
                myAuctionItemButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DataCenter.position = position;
                        ((BaseActionBarActivity)context).showProgressDialog();
                        seconds = System.currentTimeMillis();
                        if(list.get(position).getTotime() != null){
                            if (seconds>=Long.parseLong(list.get(position).getTotime())){
                                Log.d("seconds","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
                                Log.d("seconds",list.get(position).getTotime());
                                Toast.makeText(DataCenter.context, "时间已过期,不能进行操作", Toast.LENGTH_SHORT).show();
                                return;
                            }
                        }
                        else {
                            DataCenter.position = position;
                            if (Integer.parseInt(list.get(position).getStatus()) == 2) {
                                new GetCloseOpen(DataCenter.userLogin.get(0).getUserid(), "open", DataCenter.beansAuction.get(position).getItemid(), "17", DataCenter.pat_category_id[0] + "").execute();
                            } else if (Integer.parseInt(list.get(position).getStatus()) == 3) {
                                new GetCloseOpen(DataCenter.userLogin.get(0).getUserid(), "close", DataCenter.beansAuction.get(position).getItemid(), "17", DataCenter.pat_category_id[0] + "").execute();
                            }
                        }
                    }
                });
            }
        }
        return convertView;
    }
}
